import turtle
import time

turtle.setup(600, 600)
turtle.bgpic('./1.gif')

questions = [
    {'q': '整数在python中用什么表示',
     'options': ["int", 'str', 'float'],
     'a': 'int'
     },
    {'q': '浮点数在python中用什么表示',
     'options': ["int", 'str', 'float'],
     'a': 'float'
     }
]
positions = []

def up_goto(x,y):
    turtle.up()
    turtle.goto(x,y)
    turtle.down()

def draw():
    turtle.clear()
    turtle.pencolor('black')
    if index == len(questions):
        turtle.write("没有题了", False, 'center', ('Arial', 20, 'bold'))
    else:
        question = questions[index]
        turtle.write(question.get('q'), False, 'center', ('Arial', 20, 'bold'))
        height = turtle.window_height()
        width = turtle.window_width()
        dis =(width-200)/(len(question.get('options'))-1)
        x = -200
        y = -100
        coding = 'ABCDEFG'
        c_i= 0
        for i in question.get('options'):
            up_goto(x,y)
            positions.append([x,y])
            turtle.write(coding[c_i]+'.'+i, False, 'center', ('Arial', 20, 'bold'))
            x+=dis
            c_i+=1

def func_click(x,y):
    global is_first, index
    if is_first:
        is_first = False
    else:
        turtle.undo()
    question = questions[index]
    if -100<y<-70:
        up_goto(0, -30)
        c_i = 0
        for i in question.get('options'):
            if positions[c_i][0]-50 <x<positions[c_i][0]+50:
                if question.get('a') == i:
                    turtle.pencolor('green')
                    turtle.write("回答正确", False, 'center', ('Arial', 20, 'bold'))
                    is_first = True
                    index+=1
                    time.sleep(2)
                    draw()
                else:
                    turtle.pencolor('red')
                    turtle.write("回答错误", False, 'center', ('Arial', 20, 'bold'))
                break
            c_i+=1
index=0
is_first =True
draw()
turtle.onscreenclick(func_click)
turtle.done()